﻿using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

using BLF.WWW_WWW.Resource;
using Model;
using Model.WWW_WWW.Resource;

namespace WebServices.WWW
{
    /// <summary>
    /// Resources 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://www.italki.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService]
    public class Resources : System.Web.Services.WebService
    {
        /// <summary>
        /// 根据学习语言,所说语言获取相关信息
        /// </summary>
        /// <param name="learnLan">学习语言</param>
        /// <param name="baseLan">所说语言</param>        
        /// <returns>ArrayList(0:List(TResourcesPageModel)使用语言学习语言资源,1:List(TResourcesPageModel)学习语言学习资源,2:List(TResourcesModel)学习语言最受欢迎的资源)</returns>
        [WebMethod]
        public ArrayList GetResources(string learnLan, string baseLan)
        {
            return BLF.WWW_WWW.Resource.Resources.GetResources(learnLan, baseLan);
        }
        /// <summary>
        /// 根据ResID获取资源信息
        /// </summary>
        /// <param name="resId">资源ID</param>
        /// <returns>TResourcesModel</returns>
        [WebMethod]
        public TResourcesModel GetResourceByResID(int resId)
        {
            return BLF.WWW_WWW.Resource.Resources.GetResourceByResID(resId);
        }
        /// <summary>
        /// 根据PageID,CateId获取资源列表
        /// </summary>
        /// <param name="pageID">资源页ID</param>
        /// <param name="cateID">分类ID</param>
        /// <returns>TResourcesModel列表</returns>
        [WebMethod]
        public System.Collections.Generic.List<TResourcesModel> GetResourcesListByPC(int pageID, int cateID)
        {
            return BLF.WWW_WWW.Resource.Resources.GetResourcesListByPC(pageID, cateID);
        }
        /// <summary>
        /// 添加资源T_Resources
        /// </summary>
        /// <param name="pageID">资源页ID</param>
        /// <param name="cateId">分类ID</param>
        /// <param name="url">资源Url</param>
        /// <param name="name">资源标题</param>
        /// <param name="description">描述</param>
        /// <param name="languageLearn">学习的语言</param>
        /// <param name="baseLanguage">所用的语言</param>
        /// <param name="totalhits">总查看次数</param>
        /// <param name="active">是否启用</param>
        /// <param name="sortID">排序ID</param>
        /// <returns>CommonMessage</returns>
        [WebMethod]
        public CommonMessage AddResource(int pageID, int cateId, string url, string name, string description, string languageLearn,string baseLanguage, int totalhits, bool active, int sortID)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "" || !System.Text.RegularExpressions.Regex.IsMatch(url, "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=~]*)?"))
            {
                return new CommonMessage();
            }
            Model.WWW_WWW.Resource.TResourcesModel rm = new Model.WWW_WWW.Resource.TResourcesModel(0, pageID, cateId, url, name, description, languageLearn, baseLanguage, totalhits, active, sortID, memberno);
            return BLF.WWW_WWW.Resource.Resources.AddResource(rm);
        }
        /// <summary>
        /// 删除资源
        /// </summary>
        /// <param name="resId">资源ID</param>
        /// <returns>CommonMessage</returns>
        [WebMethod]
        public CommonMessage DeleteResource(int resId)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                return new CommonMessage();
            }
            return BLF.WWW_WWW.Resource.Resources.DeleteResource(resId);
        }
        
        /// <summary>
        /// 更新资源总查看数(T_Resources:Totalhits)
        /// </summary>
        /// <param name="resId">资源ID(ResId)</param>
        /// <returns>CommonMessage</returns>
        [WebMethod]
        public CommonMessage UpdateResourceHits(int resId)
        {
            return BLF.WWW_WWW.Resource.Resources.UpdateResourceHits(resId);
        }
        /// <summary>
        /// 根据ResID更新资源信息（CateID,Url,Name,Description）
        /// </summary>
        /// <param name="resID">资源ID</param>
        /// <param name="cateID">分类ID</param>
        /// <param name="url">资源URL</param>
        /// <param name="name">资源名称</param>
        /// <param name="desc">资源描述</param>
        /// <returns>CommonMessage</returns>
        [WebMethod]
        public CommonMessage UpdateResources(int resID, int cateID, string url, string name, string desc)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "" || !System.Text.RegularExpressions.Regex.IsMatch(url, "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=~]*)?"))
            {
                return new CommonMessage();
            }
            Model.WWW_WWW.Resource.TResourcesModel rm = new TResourcesModel();
            rm.ResID = resID;
            rm.CateID = cateID;
            rm.Url = url;
            rm.Name = name;
            rm.Description = desc;
            return BLF.WWW_WWW.Resource.Resources.UpdateResources(rm);
        }
        /// <summary>
        /// 根新资源信息（Active）
        /// </summary>
        /// <param name="resId">ResID</param>
        /// <param name="active">Active</param>
        /// <returns></returns>
        [WebMethod]
        public CommonMessage UpdateResourceActive(int resId, bool active)
        {            
            if (HttpContext.Current.User.Identity.Name == "")
            {
                return new CommonMessage();
            }
            return BLF.WWW_WWW.Resource.Resources.UpdateResourceActive(resId, active);
        }
        /// <summary>
        /// 根新资源信息（SortID）
        /// </summary>
        /// <param name="resId">资源ID</param>
        /// <returns>CommonMessage</returns>
        [WebMethod]
        public CommonMessage UpdateResourceSortID(int resId)
        {
            if (HttpContext.Current.User.Identity.Name == "")
            {
                return new CommonMessage();
            }
            return BLF.WWW_WWW.Resource.Resources.UpdateResourceSortID(resId);
        }
        //Admin--------------------------------------------------------------------
        /// <summary>
        /// 获取分页资源列表和资源数量(0：分页信息，1：资源列表)
        /// </summary>
        /// <param name="lanLearn">学习语言</param>
        /// <param name="Lanbase">使用语言</param>
        /// <param name="catid">分类ID（100表示所有）</param>
        /// <param name="active">是否启用</param>
        /// <param name="pageIndex">pageIndex</param>
        /// <param name="pageSize">pageSize</param>        
        /// <returns>TResourcesModel列表</returns>
        [WebMethod]
        public ArrayList GetResourcesList(string lanLearn, string Lanbase, int catid, bool active, int pageIndex, int pageSize)
        {            
            return BLF.WWW_WWW.Resource.Resources.GetResourcesList(lanLearn, Lanbase, catid, active, pageIndex, pageSize);
        }
        /// <summary>
        /// 根据学习语言,使用语言获取资源页信息（PageID，PageTitle）
        /// </summary>
        /// <param name="Learnlan">学习语言</param>
        /// <param name="baseLan">使用语言</param>
        /// <returns>TResourcesPageModel</returns>
        [WebMethod]
        public TResourcesPageModel GetResourcesPageToDT(string Learnlan, string baseLan)
        {
            return BLF.WWW_WWW.Resource.Resources.GetResourcesPageToDT(Learnlan, baseLan);
        }
        /// <summary>
        /// 根据ResID更新资源信息（CateID,Url,Name,Description,PageID,LanguageLearn,BaseLanguage,Active,SortID）
        /// </summary>        
        /// <returns>CommonMessage</returns>
        [WebMethod]
        public CommonMessage UpdateResourcesAdmin(int resID, int pageID, int cateId, string url, string name, string description, string languageLearn, string baseLanguage, bool active, int sortID)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "" || !System.Text.RegularExpressions.Regex.IsMatch(url, "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=~]*)?") || resID <= 0 || pageID <= 0)
            {
                return new CommonMessage();
            }
            Model.WWW_WWW.Resource.TResourcesModel rm = new TResourcesModel();
            rm.PageID = pageID;
            rm.ResID = resID;
            rm.CateID = cateId;
            rm.Url = url;
            rm.Name = name;
            rm.Description = description;
            rm.LanguageLearn = languageLearn;
            rm.BaseLanguage = baseLanguage;
            rm.Active = active;
            rm.SortID = sortID;
            return BLF.WWW_WWW.Resource.Resources.UpdateResourcesAdmin(rm);
        }
        /// <summary>
        /// 根据PageID更新资源页信息（PageTitle,LanguageLearn,BaseLanguage,Description）
        /// </summary>        
        /// <returns>CommonMessage</returns>
        [WebMethod]
        public CommonMessage UpdateResourcesPageAdmin(int pageID, string pageTitle, string learnLan, string baseLan, string desc)
        {
            if (HttpContext.Current.User.Identity.Name == "")
            {
                return new CommonMessage();
            }
            TResourcesPageModel rpage = new TResourcesPageModel();
            rpage.PageID = pageID;
            rpage.PageTitle = pageTitle;
            rpage.LanguageLearn = learnLan;
            rpage.BaseLanguage = baseLan;
            rpage.Description = desc;
            return BLF.WWW_WWW.Resource.Resources.UpdateResourcesPageAdmin(rpage);
        }
        /// <summary>
        /// 添加资源页
        /// </summary>        
        /// <returns>CommonMessage</returns>
        [WebMethod]
        public CommonMessage AddResourcesPage(string pageTitle, string learnLan, string baseLan, string desc, int totalhits, int dayhits, int weekHits, int monthHits)
        {
            if (HttpContext.Current.User.Identity.Name == "")
            {
                return new CommonMessage();
            }
            TResourcesPageModel rpage = new TResourcesPageModel();            
            rpage.PageTitle = pageTitle;
            rpage.LanguageLearn = learnLan;
            rpage.BaseLanguage = baseLan;
            rpage.Description = desc;
            rpage.Totalhits = totalhits;
            rpage.DayHits = dayhits;
            rpage.WeekHits = weekHits;
            rpage.MonthHits = monthHits;
            return BLF.WWW_WWW.Resource.Resources.AddResourcesPage(rpage);
        }
    }
}

